<?php
namespace Modules\Skinov;

use Phalcon\DiInterface;

class Module implements \Phalcon\Mvc\ModuleDefinitionInterface {
  public function registerAutoloaders(DiInterface $di = null) {
    (new \Phalcon\Loader()) -> registerNamespaces([
      'Components' => APPLICATION_SRC_DIR . '/components/',

      'BaconQrCode' => APPLICATION_ROOT_DIR . '/vendor/bacon/bacon-qr-code/src/BaconQrCode/',
      'EasyWeChat' => APPLICATION_ROOT_DIR . '/vendor/overtrue/wechat/src/',
      'EasyWeChatComposer' => APPLICATION_ROOT_DIR . '/vendor/easywechat-composer/easywechat-composer/src/',
      'Endroid\\QrCode' => APPLICATION_ROOT_DIR . '/vendor/endroid/qr-code/src/',
      'GuzzleHttp' => APPLICATION_ROOT_DIR . '/vendor/guzzlehttp/guzzle/src/',
      'GuzzleHttp\\Promise' => APPLICATION_ROOT_DIR . '/vendor/guzzlehttp/promises/src/',
      'GuzzleHttp\\Psr7' => APPLICATION_ROOT_DIR . '/vendor/guzzlehttp/psr7/src/',
      'Monolog' => APPLICATION_ROOT_DIR . '/vendor/monolog/monolog/src/Monolog/',
      'MyCLabs\Enum' => APPLICATION_ROOT_DIR . '/vendor/myclabs/php-enum/src/',
      'Overtrue\\Socialite' => APPLICATION_ROOT_DIR . '/vendor/overtrue/socialite/src/',
      'Pimple' => APPLICATION_ROOT_DIR . '/vendor/pimple/pimple/src/Pimple/',
      'Psr\\Cache' => APPLICATION_ROOT_DIR . '/vendor/psr/cache/src/',
      'Psr\\Container' => APPLICATION_ROOT_DIR . '/vendor/psr/container/src/',
      'Psr\\Log' => APPLICATION_ROOT_DIR . '/vendor/psr/log/Psr/Log/',
      'Psr\\SimpleCache' => APPLICATION_ROOT_DIR . '/vendor/psr/simple-cache/src/',
      'Psr\\Http\\Message' => APPLICATION_ROOT_DIR . '/vendor/psr/http-message/src/',
      'Symfony\\Component\\Cache' => APPLICATION_ROOT_DIR . '/vendor/symfony/cache/',
      'Symfony\\Component\\HttpFoundation' => APPLICATION_ROOT_DIR . '/vendor/symfony/http-foundation/',

      __NAMESPACE__ . '\\Controllers' => __DIR__ . '/controllers/',
      __NAMESPACE__ . '\\Models' => __DIR__ . '/models/',
      __NAMESPACE__ . '\\Services' => __DIR__ . '/services/',
    ])
    -> registerFiles([
      APPLICATION_ROOT_DIR . '/vendor/guzzlehttp/guzzle/src/functions_include.php',
      APPLICATION_ROOT_DIR . '/vendor/guzzlehttp/promises/src/functions_include.php',
      APPLICATION_ROOT_DIR . '/vendor/guzzlehttp/psr7/src/functions_include.php'
    ])
    -> register();
  }

  public function registerServices(DiInterface $di) {
    $di -> setShared('dispatcher', function() {
      $dispatcher = new \Phalcon\Mvc\Dispatcher();
      $dispatcher -> setDefaultNamespace(__NAMESPACE__ . '\\Controllers');
      return $dispatcher;
    });

    $di -> setShared('view', function () {
      return (new \Phalcon\Mvc\View()) -> disable();
    });

    $di -> setShared('config', function() {
      $config = new \Phalcon\Config(require(APPLICATION_SRC_DIR . '/config/' . APPLICATION_ENV . '.php'));
      $config -> merge(new \Phalcon\Config(require(__DIR__ . '/config/' . APPLICATION_ENV . '.php')));
      return $config;
    });

    $di -> setShared('session', function() {
      $session = new \Phalcon\Session\Adapter\Files();
      $session -> start();
      return $session;
    });

    $di -> setShared('db', function () {
      $db = $this -> getShared('config') -> get('DATABASE_MASTER') -> get(0);

      $class = 'Phalcon\\Db\\Adapter\\Pdo\\' . $db -> adapter;
      return new $class([
        'host'     => $db -> host,
        'username' => $db -> username,
        'password' => $db -> password,
        'port'     => $db -> port,
        'dbname'   => $db -> dbname,
        'charset'  => $db -> charset
      ]);
    });

    $di -> setShared('crypt', function () {
      return (new \Phalcon\Crypt()) -> setKey($this -> getShared('config') -> CRYPT_KEY);
    });

    $di -> setShared('easyWeChat', function () {
      return \EasyWeChat\Factory::officialAccount([
        // 测试
        //'app_id' => 'wx556e3929f865d091',
        //'secret' => '23c057aa7db710b491a76d21e3eeae69',

        // skinov丝亘诺芙
        //'app_id' => 'wxb947c4625d2223f4',
        //'secret' => '91730e96a41b54e27bf0d80a9964c5dd',

        // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',

        'log' => [
          'level' => 'debug',
          'file' => $this -> get('config') -> RUNTIME_LOGGER_DIR . '/skinov/wechat.log'
        ],
      ]);
    });
  }
}
